package com.example.room;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;

import com.example.room.adapter.StudentRecycleViewAdapter;
import com.example.room.dao.StudentDao;
import com.example.room.database.StudentDataBase;
import com.example.room.entity.Student;

import java.util.ArrayList;
import java.util.List;

public class StudentInfoActivity extends AppCompatActivity {

    private StudentRecycleViewAdapter adapter;
    private List<Student> list;
    private RecyclerView rv_student;
    private StudentDao dao;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_info);
        rv_student = findViewById(R.id.rv_student);
        list = new ArrayList<>();
        adapter = new StudentRecycleViewAdapter(list);
        rv_student.setAdapter(adapter);
        rv_student.setLayoutManager(new LinearLayoutManager(this));

        StudentDataBase dataBase = StudentDataBase.getInstance(this);
        dao = dataBase.getDao();
    }

    //增加
    public void addStudentInfo(View view) {
        Student s1 = new Student("jack", 19);
        new insertStudentTask(dao).execute(s1);
    }

    public void deleteAllInfo(View view) {
    }

    public class insertStudentTask extends AsyncTask<Student, Void, Void> {

        private StudentDao studentDao;

        public insertStudentTask(StudentDao studentDao) {
            this.studentDao = studentDao;
        }

        @Override
        protected Void doInBackground(Student... students) {
            studentDao.insetStudent(students);
            return null;
        }
    }

    //删除
    public void deleteStudentInfo(View view) {
        Student s3 = new Student(5);
        new deleteStudentTask(dao).execute(s3);
    }

    public class deleteStudentTask extends AsyncTask<Student , Void , Void>{
        private StudentDao studentDao;

        public deleteStudentTask(StudentDao studentDao) {
            this.studentDao = studentDao;
        }

        @Override
        protected Void doInBackground(Student... students) {
            studentDao.deleteStudent(students);
            return null;
        }
    }


    //修改
    public void updateStudentInfo(View view) {
        Student s2 = new Student(3, "Jason", 30);
        new updateStudentTask(dao).execute(s2);
    }

    public class updateStudentTask extends AsyncTask<Student, Void, Void> {
        private StudentDao studentDao;

        public updateStudentTask(StudentDao studentDao) {
            this.studentDao = studentDao;
        }

        @Override
        protected Void doInBackground(Student... students) {
            studentDao.updateStudent(students);
            return null;
        }
    }

    //查询
    public void queryStudentInfo(View view) {
        new queryAllTask(dao).execute();
    }

    public class queryAllTask extends AsyncTask<Void, Void, Void> {

        private StudentDao studentDao;

        public queryAllTask(StudentDao studentDao) {
            this.studentDao = studentDao;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            List<Student> listLiveData = studentDao.queryAll();
            adapter.setList(listLiveData);
            return null;
        }

        @Override
        protected void onPostExecute(Void unused) {
            super.onPostExecute(unused);
            adapter.notifyDataSetChanged();
        }
    }
}